/*----------------------------------------------------------------------------
 * Top Menu
 * ---------------------------------------------------------------------------
 * Date      : 2011-02-08
 * Author    :  naiyumie
 * ---------------------------------------------------------------------------
 * USAGE
 * ---------------------------------------------------------------------------
 * $(selector).TopMenu();
 * ---------------------------------------------------------------------------
 * EXAMPLE
 * ---------------------------------------------------------------------------
 *
 *----------------------------------------------------------------------------
 */
$.fn.TopMenu	= function (option)
{
	var changeImage	= function (obj, ext, action)
	{
		var img	= $(obj);
		var src	= $(img).attr('src');

		if (action=='over') { src	= src.replace(ext, '_on' + ext); }
		else { src	= src.replace('_on' + ext, ext); }

		$(img).attr('src', src);
	};

	var changeImage2	= function (obj, ext, action)
	{
		var img	= $(obj);
		var src	= $(img).attr('src');

		if (action=='over') { src	= src.replace('_on' + ext, '_over' + ext); }
		else { src	= src.replace('_over' + ext, '_on' + ext); }

		$(img).attr('src', src);
	};

	$(this).hover
	(
		function()
		{
			$(this).addClass('subbg');
			$(this).find('ul.lnb-sub-hidden').addClass('lnb-sub').removeClass('lnb-sub-hidden');
			$(this).children('ul').children('li').css('height', $(this).height());

		},
		function()
		{
			$(this).removeClass('subbg');
			$(this).find('ul.lnb-sub').addClass('lnb-sub-hidden').removeClass('lnb-sub');
			$(this).children('ul').children('li').css('height', '');
		}
	);

	$(this).children('ul').children('li').each(function(index)
	{
		index++;

		$(this).hover
		(
			function()
			{
				changeImage($(this).children('a:first').children('img:first'), '.gif', 'over');

				$(this).children('ul:first').addClass('lnb-sub-bg' + index).children('li').each(function()
				{
					changeImage($(this).find('img'), '.gif', 'over');
				});

				$(this).addClass('active');

				$(this).children('ul:first').children('li').each(function()
				{
					$(this).children('a:first').hover
					(
						function ()
						{
							changeImage2($(this).children('img:first'), '.gif', 'over');
						},
						function ()
						{
							changeImage2($(this).children('img:first'), '.gif');
						}
					);
				});
			},
			function()
			{
				$(this).children('ul:first').children('li').each(function()
				{
					$(this).children('a:first').unbind('hover');
				});

				changeImage($(this).children('a:first').children('img:first'), '.gif');

				$(this).children('ul:first').removeClass('lnb-sub-bg' + index).children('li').each(function()
				{
					changeImage($(this).find('img'), '.gif');
				});

				$(this).removeClass('active');

			}
		);
	});
};